package com.jie.other.jetpack

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.jie.commonlib.base.BaseActivity
import com.jie.other.BR
import com.jie.other.R
import com.jie.other.bean.ui.ViewModelItemBean
import com.jie.other.databinding.OtherActivityDataBindingBinding
import com.jie.other.databinding.OtherAdapterDataBindingListBinding
import com.jie.other.jetpack.viewmodel.UserViewModel
import kotlin.random.Random

/**
 * DataBinding框架学习测试页面
 */
class DataBindingActivity : BaseActivity() {

    private lateinit var mViewBinding : OtherActivityDataBindingBinding
    private val mUserViewMode by viewModels<UserViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initActionBar()
        initBinding()
        setViewModel()
    }

    private fun initActionBar() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setTitle(R.string.other_data_binding)
    }

    private fun initBinding() {
        mViewBinding = DataBindingUtil.setContentView(this, R.layout.other_activity_data_binding)
        mViewBinding.setVariable(BR.clickListener, ClickListener(mUserViewMode))
        //结合DataBinding使用的ViewModel
        //要使用ViewModel对象作为数据绑定来源，需要设置LifecycleOwner
        mViewBinding.lifecycleOwner = this
        mViewBinding.setVariable(BR.userViewModel, mUserViewMode)
        mViewBinding.rvList.adapter = ListAdapter(mUserViewMode.getContentList())
    }

    private fun setViewModel() {
        mUserViewMode.getContentListChange().observe(this) {
            mViewBinding.rvList.adapter?.notifyDataSetChanged()
        }
    }

    class ClickListener(private val userViewModel: UserViewModel) {

        fun addUser(view : View) {
            userViewModel.addUser(ViewModelItemBean("name:${Random.nextInt(1000)}"))
        }

        fun removeUser(view : View) {
            userViewModel.removeUser(0)
        }
    }

    class ListAdapter(private val mList : MutableList<ViewModelItemBean>?) : RecyclerView.Adapter<ListViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
            return ListViewHolder(DataBindingUtil.inflate<OtherAdapterDataBindingListBinding>(
                LayoutInflater.from(parent.context),
                R.layout.other_adapter_data_binding_list,
                parent,
                false
            ))
        }

        override fun getItemCount(): Int {
            return mList?.size?:0
        }

        override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
            holder.viewDataBinding.setVariable(BR.item, mList!![position])
        }

    }

    class ListViewHolder(val viewDataBinding: ViewDataBinding)
        : RecyclerView.ViewHolder(viewDataBinding.root)
}